package org.boyan.trans;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.boyan.entity.query.QueryInterface;
import org.boyan.entity.trans.Trans;
import org.boyan.utils.ExcelUtils;

import java.io.File;
import java.util.List;

public class ExcelToTrans {

    //将引入的Excel文件转换成交易对象
    public static Trans getTrans(Sheet sheet){
        Trans trans=new Trans();
        try{
            trans.setTransName(sheet.getRow(0).getCell(1).getStringCellValue());
            trans.setTransCode(sheet.getRow(1).getCell(1).getStringCellValue());
            trans.setQuerySql(getSql(sheet));
            List<QueryInterface> input=ExcelToQueryInterface.getInput(sheet);
            trans.setInput(input);
            List<QueryInterface> output=ExcelToQueryInterface.getOutput(sheet);
            trans.setOutput(output);
            List<QueryInterface> arrayObject=ExcelToQueryInterface.getArrayObject(sheet);
            trans.setArrayObject(arrayObject);
        }catch (Exception e){
            e.printStackTrace();
        }
        return trans;
    }

    private static String getSql(Sheet sheet){
        for(Row row:sheet){
            if(row.getCell(0)==null){
                continue;
            }
            if(row.getCell(0).getStringCellValue().startsWith("查询SQL语句")){
                String sql=row.getCell(0).getStringCellValue().replace("查询SQL语句(必填):","");
                return sql;
            }
        }
        return "";
    }


    //获取输入项的起始row
    private static int getInputRow(Sheet sheet){
        for(Row row:sheet){
            if("输入".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取输出项的起始row
    private static int getOutputRow(Sheet sheet){
        for(Row row:sheet){
            if("输出".equals(row.getCell(0).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取表对象的起始row
    private static int getArrayStartRow(Sheet sheet){
        for(Row row:sheet){
            if("start".equalsIgnoreCase(row.getCell(4).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

    //获取表对象的结束row
    private static int getArrayEndRow(Sheet sheet){
        for(Row row:sheet){
            if("end".equalsIgnoreCase(row.getCell(4).getStringCellValue())){
                return row.getRowNum();
            }
        }
        return -1;
    }

}
